// 用户模块
const express=require('express')
const router=express.Router()
const pool=require('../pool.js')
module.exports=router
// 用户登录 
router.post('/login',(req,res,next)=>{
	let $uname=req.body.uname
	let $upwd=req.body.upwd
	// 用户名和密码非空验证
	if(!$uname || !$upwd){
		res.send({code:201,msg:'uname or upwd invalid!'})
		return;
	}
	// 执行sql语句
	pool.query('SELECT uid FROM xz_user WHERE uname=? && upwd=?',[$uname,$upwd],(err,result)=>{
		if(err){
			next(err)
			return
		}
		if(result.length){
			res.send({code:200,msg:'login success',uid:result[0].uid})
		}else{
			res.send({code:202,msg:'login error'})
		}
	})
})
// 验证用户名是否可用
router.get('/check_uname',(req,res,next)=>{
	let $uname=req.query.uname;
	pool.query('SELECT uid FROM xz_user WHERE uname=?',[$uname],(err,result)=>{
		if(err){
			next(err)
			return
		}
		if(!result.length){
			res.send({code:200,msg:'non-exists'})
		}else{
			res.send({code:201,msg:'exists'})
		}
	})
})
// 验证邮箱是否可用
router.get('/check_email',(req,res,next)=>{
	let $email=req.query.email;
	pool.query('SELECT uid FROM xz_user WHERE email=?',[$email],(err,result)=>{
		if(err){
			next(err);
			return
		}
		if(!result.length){
			res.send({code:200,msg:'non-exists'})
		}else {
			res.send({code:201,msg:'exists'})
		}
	})
})
// 验证手机号是否可用
router.get('/check_phone',(req,res,next)=>{
	let $phone=req.query.phone;
	pool.query('SELECT uid FROM xz_user WHERE phone=?',[$phone],(err,result)=>{
		if(err){
			next(err)
			return
		}
		if(!result.length){
			res.send({code:200,msg:'non-exists'})
		}else{
			res.send({code:201,msg:'exists'})
		}
	})
})
// 用户注册接口
router.post('/register',(req,res,next) => {
	let {uname,upwd,email,phone} = req.body
	let code = 400 
	// 验证客户端传递的数据是否为空
	for(let key in req.body) {
		code++
		if(!req.body[key]) {
			res.send({code:code,msg:`${key} invalid value`})
			return
		}
	}
	pool.query('SELECT uid FROM xz_user WHERE uname=?',[uname],(err,result)=>{
		if(err) {
			next(err)
			return
		}
		if(result.length != 0){
			res.send({code:401,msg:'uname exists'})
		}else {
			if(!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)){
				res.send({code:401,msg:'email invalid'})
				return
			}
			pool.query('SELECT uid FROM xz_user WHERE email=?',[email],(err,result)=> {
				if(err) {
					next(err)
					return
				}
				if(result.length != 0) {
					res.send({code:402,msg:'email exists'})
				}else {
					pool.query('SELECT uid FROM xz_user WHERE phone=?',[phone],(err,result) => {
						if(err){
							next(err)
							return
						}
						if(result.length != 0) {
							res.send({code:403,msg:'phone exists'})
						}else {
							pool.query('INSERT INTO xz_user(uname,upwd,email,phone) VALUES(?,?,?,?)',[uname,upwd,email,phone],(err,result) => {
								if(err){
									next(err)
									return
								}
								res.send({code:200,msg:'register success'})
							})
						}
					})
				}
			})
		}
	})
})
// 检索用户
router.get('/user_basis/:uid',(req,res,next) => {
	pool.query('SELECT uname,user_name,email,phone FROM xz_user WHERE uid=?',[req.params.uid],(err,result) => {
		if(err) {
			next(err)
			return 
		}
		res.send({code:200,data:result})
	})
})
// 用户修改信息
router.put('/user_put',(req,res,next) => {
	let {uid,user_name,email,phone,gender} = req.body
	pool.query('UPDATE xz_user SET user_name=?,email=?,phone=?,gender=? WHERE uid=?',[user_name,email,phone,gender,uid],(err,result)=>{
		if(err) {
			next(err)
			return
		}
		if(result.changedRows != 0) {
			res.send({code:200,msg:'update success'})
		}else {
			res.send({code:401,msg:'update error'})
		}
	})
})